package sueldos;

import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;


public class BeneficiariosSeguro {

	
	public Empleado getEmpleadoDB(long legajo, String empresa){
		try {
			Connection conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+empresa, "", ""); 
			String query="SELECT LEGAJO, APELLIDO, NOMBRES,DATE_FORMAT(fechanaci,'%d-%m-%Y') AS fecha,NACIONALID, " +
					"TIPODOCU,NRODOCU,SEXO,ESTADOCIVI,CUIL,DOMICILIO,NUMERO,PISO,DPTO,LOCALIDAD,CODIGOPOST,PROVINCIA," +
					"TAREA,DATE_FORMAT(FECHAING,'%d-%m-%Y') AS Ingreso FROM empleado WHERE (LEGAJO = ?)";
			PreparedStatement ps = conexion.prepareStatement(query);
			ps.setLong(1, legajo);
			ps.execute();
			ResultSet rs = ps.getResultSet();
			while (rs.next()){
				return new Empleado(rs.getLong(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getString(6),rs.getString(7), rs.getString(8) ,rs.getString(9),rs.getString(10), rs.getString(19)) {
				};
			}
			ps.close();
			conexion.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;

	}
	
	
    		
    public static void main(String[] args) throws IOException, DocumentException {
    		
    	//	for (int i = 1; i < 80; i++) {
				
			
    	
    		Empleado e = new BeneficiariosSeguro().getEmpleadoDB(197, "clavier");
    		
    		String filename = e.getLegajo()+" "+e.getNombre()+" "+e.getApellido();
    		String ruta = "C:/Users/Juani/Desktop/";
    		String extension = ".pdf";
    		String output = ruta+filename+extension;
            
    		String comando[] = {"C:/Program Files/Adobe/Reader 11.0/Reader/AcroRd32.exe", output };
    		
    		PdfReader reader = new PdfReader("E:/167 - La Caja/Empleados Beneficiarios - La Caja 2.pdf");
            PdfStamper filledOutForm = new PdfStamper(reader, new FileOutputStream(output));
            
            AcroFields form = filledOutForm.getAcroFields();
            form.setField("legajo", "60");
            form.setField("poliza", "5800000889901");
            form.setField("empresa", "Clavier SA");
            form.setField("dirempresa", "Uruguay 911 2 A");
            form.setField("nomeApe", e.getApellido()+", "+e.getNombre());
            form.setField("fecnac", e.getFechaNac());
 
            if (e.getSexo().equals("M"))
            	form.setField("male", "Yes");
            else
            	form.setField("female", "Yes");

            form.setField("doctipoynro", e.getTipodoc()+" "+e.getDocumento());
            form.setField("cuil", e.getCuil());
            form.setField("argentino", "Yes");
            form.setField("provincia", "");
            form.setField("fecingreso", e.getFechaIngreso().substring(0, 6)+e.getFechaIngreso().substring(8, 10));
            form.setField("segurVida","Yes");
             
           
            
            filledOutForm.setFormFlattening(true);
            
            
            
            filledOutForm.close();
            

            
            //Runtime.getRuntime().exec(comando);
            
            
    	//	}
            
    }

}




